package routers

import (
	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/filter/cors"
	"house/controllers"
)

func init() {
	// 跨域
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{AllowAllOrigins: true}))

	beego.Router("/login", &controllers.UserController{}, "post:Login")

	beego.Router("/send/code", &controllers.UserController{}, "post:Code")

	contract := beego.NewNamespace("/v1",
		// 在内部再次需要定义子路由组的时候。 可使用
		// NS 级别的Namespace 去定义 理论上Namespace是可以无限进行嵌套的
		beego.NSNamespace("/contract",
			beego.NSRouter("/get/all", &controllers.ContractController{}, "get:GetAllContracts"),
			beego.NSRouter("/add", &controllers.ContractController{}, "post:AddContract"),
			beego.NSRouter("/get/id", &controllers.ContractController{}, "get:GetContractById"),
		),
	)

	house := beego.NewNamespace("/v1",
		// 在内部再次需要定义子路由组的时候。 可使用
		// NS 级别的Namespace 去定义 理论上Namespace是可以无限进行嵌套的
		beego.NSNamespace("/house",
			beego.NSRouter("/get/all", &controllers.HouseController{}, "get:GetAllHouse"),
			beego.NSRouter("/add", &controllers.HouseController{}, "post:AddHouse"),
			beego.NSRouter("/get/id", &controllers.HouseController{}, "get:GetHouseById"),
		),
	)

	//注册路由组
	beego.AddNamespace(contract, house)
	beego.Router("/login", &controllers.HouseController{}, "post:Login")
	//beego.Router("/user/info", &controllers.HouseController{}, "post:Login")
	beego.Router("/send/code", &controllers.HouseController{}, "post:Code")
}
